import { createRouter, createWebHashHistory } from "vue-router";
import learningRouter from "./learning";

const router = createRouter({
  history: createWebHashHistory(),
  routes: [
    ...learningRouter,
    {
      path: "/home",
      name: "Home",
      component: () => import("@/views/home/home-index.vue"),
    },
    {
      path: "/",
      redirect: "/home",
    },
  ],
});

// 全局路由前置守卫
router.beforeEach((to, from, next) => {
  console.log("beforeEach");
  console.log("to", to);
  console.log("from", from);
  next();
  // if (to.name !== 'Home' && from.name === 'Home') {
  //     console.log('首页跳转到其他页面时，进行拦截到首页')
  //     next({ name: 'Home' })
  // } else {
  //     next()
  // }
});

// 全局解析
router.beforeResolve((to, from, next) => {
  console.log("beforeResolve");
  console.log("to", to);
  console.log("from", from);
  next();
});

// 全局后置钩子
router.afterEach((to, from) => {
  console.log("afterEach");
  console.log("to", to);
  console.log("from", from);
});

export default router;
